package com.hms.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hms.entity.Admin;
import com.hms.entity.Resource;
import com.hms.entity.Role;
import com.hms.pojo.MenuPojo;
import com.hms.service.IAdminService;
import com.hms.service.IResourceService;

@Controller
@RequestMapping("/menu")
public class MenuController extends BaseController {

	@javax.annotation.Resource
	IAdminService adminService;

	@javax.annotation.Resource
	IResourceService resourceService;

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@ResponseBody
	public List<MenuPojo> menu() {
		// 获取已经登录账号
		Admin loginAdmin = adminService.getLoginAdmin();
		if (loginAdmin == null) {
			return null;
		}
		loginAdmin = adminService.getAdminByuUserName(loginAdmin.getUsername());

		Set<Role> roles = loginAdmin.getRoleSet();

		List<MenuPojo> menus = new ArrayList<MenuPojo>();

		List<Resource> allResource = resourceService.getAll();
		for (Resource resource : allResource) {
			List<Resource> resouces = new ArrayList<Resource>();
			for (Role role : roles) {
				Set<Resource> resources = role.getResourceSet();
				for (Resource roleResource : resources) {
					if (resource.getId().equals(roleResource.getId())) {
						resouces.add(resource);
					}
				}
			}
			if (resouces.size() > 0) {
				menus.add(new MenuPojo(resource, resouces));
			}
		}
		return menus;
	}
}
